﻿using System;
using System.Collections.Generic;
using System.Text;
using eBlog.Entity;
using DBScript;
namespace eBlog.Service
{
   public class LinksService
   {
        /// <summary>
        /// 列表
        /// </summary>
        private static List<Links> _links;

        /// <summary>
        /// lock
        /// </summary>
        private static object lockHelper = new object();

        static LinksService()
        {
            LoadLink();
        }


        /// <summary>
        /// 初始化
        /// </summary>
        public static void LoadLink()
        {
            if (_links == null)
            {
                lock (lockHelper)
                {
                    if (_links == null)
                    {
                        _links = GetLinkList();

                    }
                }
            }
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public static int InsertLink(Links link)
        {
            IDBBase dal = DBFactory.GetDbProvider();
             link.ID = dal.Add(link);
            _links.Add(link);
            _links.Sort();

            return link.ID;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public static bool UpdateLink(Links link)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            _links.Sort();
            return dal.Update(link);
        }

       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="linkId"></param>
       /// <returns></returns>
       public static int DeleteLink(int linkId)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            Links link = dal.GetModel<Links>(linkId);
            if (link != null)
            {
                _links.Remove(link);
            }
            return dal.Delete<Links>(linkId);
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="linkId"></param>
        /// <returns></returns>
        public static Links GetLink(int linkId)
        {

            foreach (Links link in _links)
            {
                if (link.ID == linkId)
                {
                    return link;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取连接列表
        /// </summary>
        /// <param name="position"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<Links> GetLinkList(int position, int status)
        {
            return GetLinkList(-1, position, status);
        }

        /// <summary>
        /// 获取连接列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="position"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<Links> GetLinkList(int type, int position, int status)
        {
            List<Links> list = _links;

            if (type != -1)
            {
                list = list.FindAll(delegate(Links link) { return link.Type == type; });
            }

            if (position != -1)
            {
                list = list.FindAll(delegate(Links link) { return link.Position == position; });
            }

            if (status != -1)
            {
                list = list.FindAll(delegate(Links link) { return link.Status == status; });
            }

            return list;
        }

        /// <summary>
        /// 获取连接列表
        /// </summary>
        /// <returns></returns>
        public static List<Links> GetLinkList()
        {
            IDBBase dal = DBFactory.GetDbProvider();
            _links = dal.OrderBy("Displayorder asc,ID asc").GetList<Links>();
            return _links;
        }
    }
}
